package com.study.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.study.common.fdfs.ImageUtil;
import com.study.common.utils.StringUtils;
import com.study.common.core.page.TableDataInfo;
import com.study.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.study.app.domain.bo.AppImageBo;
import com.study.app.domain.vo.AppImageVo;
import com.study.app.domain.AppImage;
import com.study.app.mapper.AppImageMapper;
import com.study.app.service.IAppImageService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * App图片管理Service业务层处理
 *
 * @author study
 * @date 2024-05-23
 */
@RequiredArgsConstructor
@Service
public class AppImageServiceImpl implements IAppImageService {

    private final AppImageMapper baseMapper;
    @Autowired
    private ImageUtil imageUtil;

    /**
     * 查询App图片管理
     */
    @Override
    public AppImageVo queryById(Long id){
        AppImageVo appImageVo = baseMapper.selectVoById(id);
        appImageVo.setImageUrl(imageUtil.location(appImageVo.getImageUrl()));
        return appImageVo;
    }

    @Override
    public AppImageVo queryBackgroundImage() {
        AppImageVo appImageVo = baseMapper.selectVoOne(Wrappers.<AppImage>lambdaQuery()
                .eq(AppImage::getStatus, AppImageBo.NORMAL)
                .eq(AppImage::getImageType, AppImageBo.BACKGROUND)
                .orderByDesc(AppImage::getUpdateTime)
                .last("limit 1"));
        appImageVo.setImageUrl(imageUtil.location(appImageVo.getImageUrl()));
        return appImageVo;
    }

    /**
     * 查询App图片管理列表
     */
    @Override
    public TableDataInfo<AppImageVo> queryPageList(AppImageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppImage> lqw = buildQueryWrapper(bo);
        Page<AppImageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<AppImageVo> records = result.getRecords();
        for (AppImageVo img :records) {
            img.setImageUrl(imageUtil.location(img.getImageUrl()));
        }

        return TableDataInfo.build(result);
    }

    /**
     * 查询App图片管理列表
     */
    @Override
    public List<AppImageVo> queryList(AppImageBo bo) {
        LambdaQueryWrapper<AppImage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppImage> buildQueryWrapper(AppImageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppImage> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getImageType() != null, AppImage::getImageType, bo.getImageType());
        lqw.like(StringUtils.isNotBlank(bo.getImageTag()), AppImage::getImageTag, bo.getImageTag());
        lqw.eq(bo.getStatus() != null, AppImage::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getImageUrl()), AppImage::getImageUrl, bo.getImageUrl());
        lqw.eq(bo.getUploadTime() != null, AppImage::getUploadTime, bo.getUploadTime());
        return lqw;
    }

    /**
     * 新增App图片管理
     */
    @Override
    public Boolean insertByBo(AppImageBo bo) {
        AppImage add = BeanUtil.toBean(bo, AppImage.class);
        validEntityBeforeSave(add);
        add.setUploadTime(new Date());
        add.setImageUrl(imageUtil.replaceUrl(bo.getImageUrl()));
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改App图片管理
     */
    @Override
    public Boolean updateByBo(AppImageBo bo) {
        AppImage update = BeanUtil.toBean(bo, AppImage.class);
        update.setImageUrl(imageUtil.replaceUrl(bo.getImageUrl()));
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppImage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除App图片管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
